package com.avidys.gwt.wom.client;


import com.avidys.common.gwt.net.ControlledWidget;
import com.avidys.common.gwt.net.Controller;
import com.avidys.common.gwt.net.JSONRPC.ServerResult;
import com.avidys.common.gwt.ui.TimerLabel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ToggleButton;
import com.google.gwt.user.client.ui.UIObject;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;

	/*
	 * A Grid to test step by step server communication
	 * incorporates SID 
	 */
	public class GameRoomPanel extends Composite implements ControlledWidget{

		String[] slots = new String[] {"J1","J2","J3","J4","J5"}; // class
		
		MyHandler handler = new MyHandler();
		Button resetButton = new Button("RESET");
		
		private Label elapsedLabel = new Label();

		private Controller controller;

		public GameRoomPanel() {
			
			VerticalPanel vp= new VerticalPanel();

			vp.add(new Label("Game Room"));

			Grid grid = new Grid (slots.length,4);
			    // You can use the CellFormatter to affect the layout of the grid's cells.
			    CellFormatter cf = grid.getCellFormatter();
			    RowFormatter rf = grid.getRowFormatter();
			    // .setWidth(0, 2, "256px");

				for (int i = 0; i < slots.length; i++) {
					rf.setVerticalAlign(i, HasVerticalAlignment.ALIGN_MIDDLE);
					grid.setText(i, 0, String.valueOf(i+1));
					grid.setText(i, 1, slots[i]);
					Label la = new Label(" ");
					la.setWidth("300");
					grid.setWidget(i, 2, la);
					ToggleButton tb = new ToggleButton("GO","Done"); 
					DOM.setElementAttribute(tb.getElement(), "id", "tb-"+i);
					//tb.addStyleDependentName("font");
					//tb.addStyleName("button-font");
					tb.setPixelSize(20, 20);
					tb.setTitle("Step "+i+"");
					tb.setTabIndex(i);
					tb.addClickHandler(handler);
					ToolTipConfig.setToolTip(tb, new ToolTipConfig("Step "+(i+1), "Press the buttons in a sequential order"));  
					grid.setWidget(i, 3, tb);
				}
				grid.setBorderWidth(1);
				grid.setCellSpacing(0);
				grid.setCellPadding(4);
				vp.add(grid);
			
				HorizontalPanel hp= new HorizontalPanel();
				resetButton.setEnabled(false);
				resetButton.addClickHandler(handler);
				DOM.setElementAttribute(resetButton.getElement(), "id", "id-reset");
				hp.setPixelSize(40, 20);
				hp.add(elapsedLabel);
				hp.setCellHorizontalAlignment(elapsedLabel, HasHorizontalAlignment.ALIGN_LEFT);
				hp.add(new TimerLabel(500));
				hp.add(resetButton);
				hp.setCellHorizontalAlignment(resetButton, HasHorizontalAlignment.ALIGN_RIGHT);
				vp.add(hp);
			initWidget(vp);
			new CheckStatus();
		}

		class MyHandler implements ClickHandler {
			int ID;
			
			public MyHandler(int iD) {
				super();
				ID = iD;
			}

			public MyHandler() { super();}

			public void onClick(ClickEvent event) {
				UIObject s= (UIObject) event.getSource(); 
				String id = DOM.getElementAttribute(s.getElement(), "id");
				//if (Properties.environment==ENVIRONMENT.DEVELOPMENT)
				//	GWT.log("Click "+event.toDebugString()+" "+ id, null);
				if (id.equals("id-reset")){
				}
			}
		}



		/* Internal timer to check server
		 * 
		 */
		private class CheckStatus {

			private Timer elapsedTimer;
			private long startTime;
			private long elapsedTime;

			public CheckStatus () {
				// Create a new timer
				elapsedTimer = new Timer () {
					public void run() {
						check();
					}
				};

				startTime = System.currentTimeMillis();
				elapsedTimer.scheduleRepeating(500);
			}

			public void stop() {
				elapsedTimer.cancel();
			}

			/**
			 * Show the current elapsed time in the elapsedLabel widget.
			 */
			private void check () {
				elapsedTime = (long)((System.currentTimeMillis() - startTime) / 1000.0);
				checkServer();
			}

			private void checkServer() {
				// TODO Auto-generated method stub
				
			}
		}



		@Override
		public void onLogicError(String error) {
			// TODO Auto-generated method stub
			
		}



		@Override
		public void onRPCError(Throwable caught) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void setController(Controller controller) {
			this.controller = controller;
		}



		@Override
		public void onTextReceived(ServerResult[] r) {
			// TODO Auto-generated method stub
			
		}



	}


